package com.kefu.controller;

import com.kefu.dto.*;
import com.kefu.service.AdminService;
import com.kefu.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private SessionService sessionService;

    /**
     * 客服登录
     */
    @PostMapping("/login")
    public ResultDTO<Map<String, Object>> login(@RequestBody AdminLoginRequest request) {
        try {
            Map<String, Object> loginInfo = adminService.login(request);
            return ResultDTO.success(loginInfo);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 获取客服信息
     */
    @GetMapping("/info")
    public ResultDTO<AdminDTO> getAdminInfo(@RequestParam String adminId) {
        try {
            AdminDTO admin = adminService.getAdminById(adminId);
            return ResultDTO.success(admin);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 更新客服状态
     */
    @PostMapping("/status")
    public ResultDTO<Boolean> updateStatus(@RequestParam String adminId, @RequestParam int status) {
        try {
            boolean success = adminService.updateStatus(adminId, status);
            return ResultDTO.success(success);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 获取会话消息
     */
    @GetMapping("/messages")
    public ResultDTO<Map<String, Object>> getMessages(@RequestParam String sessionId, @RequestParam String adminId) {
        try {
            Map<String, Object> messages = sessionService.getSessionMessages(sessionId, adminId);
            return ResultDTO.success(messages);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 标记消息为已读
     */
    @PostMapping("/read")
    public ResultDTO<Boolean> markAsRead(@RequestParam String sessionId, @RequestParam String adminId) {
        try {
            boolean success = sessionService.markMessagesAsRead(sessionId, adminId);
            return ResultDTO.success(success);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

    /**
     * 结束会话
     */
    @PostMapping("/end-session")
    public ResultDTO<Boolean> endSession(@RequestParam String sessionId, @RequestParam String adminId) {
        try {
            // 操作类型：2表示客服
            boolean success = sessionService.endSession(sessionId, adminId, 2);
            return ResultDTO.success(success);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }


    /**
     * 获取所有有未读消息的会话（供客服查看）
     */
    @GetMapping("/unread-sessions")
    public ResultDTO<List<SessionDTO>> getAllUnreadSessions() {
        return ResultDTO.success(sessionService.getAllUnreadSessions());
    }

    /**
     * 客服绑定会话（点击读取消息时调用）
     */
    @PostMapping("/bind-session")
    public ResultDTO<SessionDTO> bindSession(@RequestBody BindSessionRequest request) {
        SessionDTO session = sessionService.bindSessionToAdmin(request.getSessionId(), request.getAdminId());
        return ResultDTO.success(session);
    }

    /**
     * 获取会话列表
     */
    @GetMapping("/sessions")
    public ResultDTO<List<SessionDTO>> getSessions(@RequestParam String adminId) {
        try {
            List<SessionDTO> sessions = sessionService.getSessionsByAdminId(adminId);
            return ResultDTO.success(sessions);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
    }

}

// 绑定会话请求参数类
class BindSessionRequest {
    private String sessionId;
    private String adminId;

    // getter和setter方法
    public String getSessionId() { return sessionId; }
    public void setSessionId(String sessionId) { this.sessionId = sessionId; }
    public String getAdminId() { return adminId; }
    public void setAdminId(String adminId) { this.adminId = adminId; }
}
